<?php

namespace App\Controller\Admin;

use App\Helper\HashHelper;
use App\Model\AuthGroup;
use App\Model\AuthUser;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Psr\Http\Message\ResponseInterface;

/**
 * @Controller(prefix="/admin/authUser")
 *
 */
class AuthUserController extends AdminBaseController
{


    /**
     * 后台管理人员列表
     * @RequestMapping(path="adminUserList")
     * @return ResponseInterface
     */
    public function adminUserList()
    {
        $model = AuthUser::from('auth_user as a')
            ->leftJoin('auth_group as b', 'a.admin_group_id', '=', 'b.id')
            ->select(['a.*', 'b.name as groupName'])
            ->orderByDesc('a.id');

        $list = page($model);

        return $this->response->returnSuccess(['list' => $list]);

    }


    /**
     * 新增用户
     * @RequestMapping(path="addAdminUser")
     * @return ResponseInterface
     */
    public function addAdminUser()
    {

        $params = $this->request->post();

        $exists = AuthUser::where(['username' => $params['username']])->exists();

        if ($exists) {
            return $this->response->adminFail('账号：' . $params['username'] . '已经存在');
        }

        $groupInfo = AuthGroup::where(['id' => $params['adminGroupId'], 'status' => 1])->first();
        if (is_null($groupInfo)) {
            return $this->response->adminFail('选择的角色组错误');
        }

        $data = [
            'username' => $params['username'],
            'nickname' => $params['nickname'],
            'admin_group_id' => $params['adminGroupId'],
            'status' => 1,
            'create_time' => time(),
            'password' => HashHelper::make($params['password']),
        ];

        $rel = AuthUser::insert($data);

        if ($rel) {
            return $this->response->adminSuccess();
        } else {
            return $this->response->adminFail();
        }


    }


    /**
     * 修改后台用户
     * @RequestMapping(path="ediAdminUser")
     * @return ResponseInterface
     */
    public function ediAdminUser()
    {
        $params = $this->request->post();
        $id = $params['id'];

        $where = [
            ['id', '!=', $id],
            ['username', '=', $params['username']]
        ];
        $exists = AuthUser::where($where)->exists();

        if ($exists) {
            return $this->response->adminFail('账号：' . $params['username'] . '已经存在');
        }


        $groupInfo = AuthGroup::where(['id' => $params['adminGroupId'], 'status' => 1])->first();
        if (is_null($groupInfo)) {
            return $this->response->adminFail('选择的角色组错误');
        }

        $data = [
            'username' => $params['username'],
            'nickname' => $params['nickname'],
            'admin_group_id' => $params['adminGroupId'],
            'status' => 1,
            'create_time' => time(),
            'password' => HashHelper::make($params['password']),
        ];

        $rel = AuthUser::where(['id' => $id])->update($data);

        if ($rel) {
            return $this->response->adminSuccess();
        } else {
            return $this->response->adminFail();
        }

    }


    /**
     * 改变用户状态
     * @RequestMapping(path="changeStatus")
     * @return ResponseInterface
     */
    public function changeStatus()
    {

        $params = $this->request->post();

        $id = $params['id'];

        $adminUser = AuthUser::where(['id' => $id])->first();

        if (is_null($adminUser)) {
            return $this->response->adminFail('用户不存在');
        }

        if ($params['status'] == $adminUser->status){
            return  $this->response->adminSuccess();
        }else{
            $adminUser->status = $params['status'];
            $rel = $adminUser->save();
            if ($rel) {
                return $this->response->adminSuccess();
            } else {
                return $this->response->adminFail();
            }

        }


    }

}